---
--类名: FatestoneShareView.lua
--创建日期: 2018-12-13
--创建人: 宋建邦
--功能描述: view
--修改内容:

local FatestoneShareView = class("FatestoneShareView", app.mvc.ViewBase)
FatestoneShareView.VIEW_ROOT_RES = Res.FatestoneShareNode

function FatestoneShareView:init()
    self:setBackGroundColorType(0)
    self:setNodeEventEnabled(true)

    self.pctl = app:getInst("PhotoController")
    self:initButtons()

    local shares = GD:queryFateStoneShares()
    local cfg = shares[tostring(self._data.cid)]

    self.img = self._root:findChild("Panel/Image")
    self.txt = self._root:findChild("Panel/text")
    self.tlt = self._root:findChild("Panel/tlt")
    self._codeImage = self._root:findChild("Panel/img_ewm")
    if true then--app:isTencent() and UD.msdkData:isNotTencentChannel() then
        self._codeImage:setVisible(false)
    else
        self._codeImage:setVisible(true)
    end
    
    self.img:loadTexture("art/fatestone/"..cfg.picture..".png")
    self.tlt:setString(L(cfg.name) )
    self.txt:setString(string.gsub(L(cfg.desc), "<br />", "\n").."")

    local psz = self._root:findChild("Panel"):getContentSize()
    local sz = self.img:getContentSize()
    local x, y = self.img:getPosition()

    self.pctl:snapshotNodes({self.img, self.txt, self.tlt, self._codeImage}, sz.width, sz.height, x - sz.width*0.5, y - sz.height*0.5)
end

function FatestoneShareView:initButtons()
    local shareConfig = {}
    cc.SpriteFrameCache:getInstance():addSpriteFrames('ui_resources/main1.plist')
    if app:getPublisher() == Enums.Publisher.ema then
        shareConfig = {
            {imagePath = Res.ShareIcons.FaceBook, callback = function()
                app.channelAPI:share(nil, Enums.ShareType.facebook, L("fb_share_title"), L("fb_share_desc"), self.pctl:getSnapShotPath())
            end},

            {imagePath = Res.ShareIcons.Twitters, callback = function()
                app.channelAPI:share(nil, Enums.ShareType.twitter, L("twt_share_title"), L("twt_share_desc"), self.pctl:getSnapShotPath())
            end},
        }
    elseif app:getPublisher() == Enums.Publisher.tencent then
        local wgPlatform = app.channelAPI:getWGPlatform()
        if wgPlatform == Enums.MSDK_PLATFORM.QQ then
            shareConfig = {
                {imagePath = Res.ShareIcons.QQ, callback = function()
                   
                    if not app.channelAPI:isPlatformInstalled(Enums.MSDK_PLATFORM.QQ) then
                        display.pushToast(L("QQNotInstalledForShare"))
                        return
                    end
                    
                    app.channelAPI:share(nil, Enums.ShareType.qqSession, L("title"), L("desc"), self.pctl:getSnapShotPath())
                    self:_onCloseShareView()
                end},
                {imagePath = Res.ShareIcons.QQZone, callback = function()
                    
                    if not app.channelAPI:isPlatformInstalled(Enums.MSDK_PLATFORM.QQ) then
                        display.pushToast(L("QQNotInstalledForShare"))
                        return
                    end
                
                    app.channelAPI:share(nil, Enums.ShareType.qqZone, L("title"), L("desc"), self.pctl:getSnapShotPath())
                    self:_onCloseShareView()
                end},
            }
        elseif wgPlatform == Enums.MSDK_PLATFORM.WX then
            shareConfig = {
                {imagePath = Res.ShareIcons.WX, callback = function()
                
                    if not app.channelAPI:isPlatformInstalled(Enums.MSDK_PLATFORM.WX) then
                        display.pushToast(L("WXNotInstalledForShare"))
                        return
                    end
                    
                    app.channelAPI:share(nil, Enums.ShareType.wxSession, L("title"), L("desc"), self.pctl:getSnapShotPath())
                    self:_onCloseShareView()
                end},
                {imagePath = Res.ShareIcons.WXZone, callback = function()
                    
                    if not app.channelAPI:isPlatformInstalled(Enums.MSDK_PLATFORM.WX) then
                        display.pushToast(L("WXNotInstalledForShare"))
                        return
                    end
                    
                    app.channelAPI:share(nil, Enums.ShareType.wxCircle, L("title"), L("desc"), self.pctl:getSnapShotPath())
                    self:_onCloseShareView()
                end},
            }
        end
    end

    for i = 1, 4 do
        local btn = self._root:findChild("Panel/btn_"..i)
        if btn then
            local cf = shareConfig[i]
            if cf then
                btn:loadTextures(cf.imagePath, cf.imagePath, cf.imagePath, 1)
                btn:addClickEventListener(function(sender)
                    cf.callback()
                end)
            else
                btn:setVisible(false)
            end
        end
    end

    self.closeBtn = self._root:findChild("Panel/Button_close")
    self.closeBtn:addClickEventListener(handler(self, self._onCloseShareView))
end


function FatestoneShareView:_onCloseShareView(sender)
    app:getInst("PhotoController"):closeFatestoneShareView()
end
function FatestoneShareView:onExit()
end

function FatestoneShareView:refresh(data)
end

return FatestoneShareView